package com.java.service;

import com.java.dao.RepositoryMapper;
import com.java.pojo.Page;
import com.java.pojo.Repository;
import com.java.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepositoryServiceImpl implements RepositoryService {

    @Autowired
    private RepositoryMapper repositoryMapper;

    @Override
    public List<Repository> getReList() {
        return repositoryMapper.getReList();
    }

    @Override
    public Page<Repository> getPageByRepository(Integer a, String rname) {
        int b=4;//显示条数
        Page<Repository> page=new Page<>();
        page.setCurryPage(a);//设置当前页码
        page.setRows(b);//设置每页显示的条数
        Integer count = repositoryMapper.getCountRepository(rname);//总条数
        page.setTotalCount(count);//总条数
        int start=(a-1)*b;/*开始的索引*/
        List<Repository> list = repositoryMapper.getListRepository(start, b, rname);
        page.setList(list);/*赋值list*/
        int totalPae=count % b==0 ? count / b : (count / b)+1;
        page.setTotalPae(totalPae);/*设置总页数*/
        if(page==null){
            page=null;
        }
        return page;
    }

    @Override
    public Integer getCount(String rname) {
        return repositoryMapper.getCountRepository(rname);
    }

    @Override
    public int addRepository(Repository repository) {
        Repository reByName = repositoryMapper.getREByName(repository.getrName());
        if(reByName==null){

        return repositoryMapper.addRepository(repository);
        }else {
            return 0;
        }
    }

    @Override
    public int delR(Integer id) {
        return repositoryMapper.delR(id);
    }

    @Override
    public Repository getREByName(String name) {
        return repositoryMapper.getREByName(name);
    }
}
